package com.example.proyyj.tool;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonUtil {
    private static JsonUtil  jsonUtil = null;//新建一个静态对象
    //Java设计模式的单例模式
    private JsonUtil(){}//私有化构造方法
    public String toJson(Object object){//Object 转 json
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        Gson gson = gsonBuilder.create();
        return gson.toJson(object);
    }

    public static JsonUtil getJsonTool() {//返回单一对象
        if(jsonUtil==null){
            jsonUtil= new JsonUtil();
        }
        return jsonUtil;
    }
}
